﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataCoreTechnologies.Scribe.Business.Html
{
    internal class TriggerDocument : Document
    {
        #region Public
        //Constructor
        internal TriggerDocument(SqlServer.Trigger trigger)
        {
            DocumentType = DocumentType.Trigger;
            if (trigger.Database != null)
            {
                RootDatabase = trigger.Database;
                RootServer = RootDatabase.Server; 
                URL = "index." + RootDatabase.Name + @".trigger." + trigger.Name + @".html";
            }
            else if (trigger.Table != null)
            {
                RootDatabase = trigger.Table.Schema.Database;
                RootServer = RootDatabase.Server;
                URL = "index." + RootDatabase.Name + "." + 
                        trigger.Table.Schema.Name + @"." + trigger.Table.Name + @".trigger." +
                        trigger.Name + @".html";
            }
            else if (trigger.View != null)
            {
                RootDatabase = trigger.View.Schema.Database;
                RootServer = RootDatabase.Server;
                URL = "index." + RootDatabase.Name + "." + 
                        trigger.View.Schema.Name + @"." + trigger.View.Name + @".trigger." +
                        trigger.Name + @".html";
            }
            _trigger = trigger;
        }
        //Properties
        internal override string DisplayName { get { return _trigger.Name; } }
        //Methods
        internal override string GetHtml()
        {
            if (_html == "")
            {
                AddDdlCssColors();

                AddCssLine(@"      	.nameColumn {width: 300px; word-wrap: break-word;}");
                AddCssLine(@"      	.propertyColumn {width: 300px; word-wrap: break-word;}");

                AddBodyLine(@"<div id=""mainBody"">");
                Boolean evenRow = true;

                if (_trigger.Database != null)
                {
                    string heirarchy = @"<h2 id=""topNav"">Location: <a href=""http://index.html"">" + RootServer.Name + @"</a> . ";
                    heirarchy += @"<a href=""http://index." + RootDatabase.Name + @".html"">"
                        + RootDatabase.Name + @"</a> . " + ScrubHtml(_trigger.Name) + @"</h2>";
                    AddBodyLine(heirarchy);
                }
                else if (_trigger.Table != null)
                {
                    string heirarchy = @"<h2 id=""topNav"">Location: <a href=""http://index.html"">" + RootServer.Name + @"</a> . ";
                    heirarchy += @"<a href=""http://index." + RootDatabase.Name + @".html"">"
                        + ScrubHtml(RootDatabase.Name) + @"</a> . ";
                    if (Business.FilterPreferences.ShowSchemasPage)
                    {
                       heirarchy += @"<a href=""http://index." + RootDatabase.Name + @".schemas.html"">" +
                           ScrubHtml(_trigger.Table.Schema.Name) + @"</a> . ";
                    }
                    else
                    {
                       heirarchy += ScrubHtml(_trigger.Table.Schema.Name) + @" . ";
                    }

                    if (Business.FilterPreferences.ShowTablesPage)
                    {
                       heirarchy += @"<a href=""http://index." + RootDatabase.Name + "." +
                           _trigger.Table.Schema.Name + @".table." +
                           _trigger.Table.Name + @".html"">" +
                           ScrubHtml(_trigger.Table.Name) + @"</a> . ";
                    }
                    else
                    {
                       heirarchy += ScrubHtml(_trigger.Table.Name) + @" . ";
                    }

                    heirarchy += _trigger.Name + @"</h2>";
                    AddBodyLine(heirarchy);
                }
                else if (_trigger.View != null)
                {
                    string heirarchy = @"<h2 id=""topNav"">Location: <a href=""http://index.html"">" + RootServer.Name + @"</a> . ";
                    heirarchy += @"<a href=""http://index." + RootDatabase.Name + @".html"">"
                        + ScrubHtml(RootDatabase.Name) + @"</a> . ";
                    if (Business.FilterPreferences.ShowSchemasPage)
                    {
                       heirarchy += @"<a href=""http://index." + RootDatabase.Name + @".schemas.html"">" +
                           ScrubHtml(_trigger.View.Schema.Name) + @"</a> . ";
                    }
                    else
                    {
                       heirarchy += ScrubHtml(_trigger.View.Schema.Name) + @" . ";
                    }

                    if (Business.FilterPreferences.ShowViewsPage)
                    {
                       heirarchy += @"<a href=""http://index." + RootDatabase.Name + "." +
                           _trigger.View.Schema.Name + @".table." +
                           _trigger.View.Name + @".html"">" +
                           ScrubHtml(_trigger.View.Name) + @"</a> . ";
                    }
                    else
                    {
                       heirarchy += ScrubHtml(_trigger.View.Name) + @" . ";
                    }

                    heirarchy += _trigger.Name + @"</h2>";
                    AddBodyLine(heirarchy);
                }

                AddBodyLine(@"<h2 id=""title"">" + ScrubHtml(_trigger.Name) + @" (Trigger)</h2>");
                #region Properties
                AddBodyLine(@"<h2>");
                AddBodyLine(@"<img id=""img_properties"" src=""" + RootFolderPath + @"minus.jpg""> ");
                AddBodyLine(@"<a href=""javascript:;"" onmousedown=""toggleDiv('properties');"">Properties</a>");
                AddBodyLine(@"</h2>");

                AddBodyLine(@"<div id=""properties"">");
                AddBodyLine(@"<table>");

                string colGroup = @"<colgroup><col class=""propertyColumn"" /><col class=""valueColumn"" /></colgroup>";
                AddBodyLine(colGroup);

                AddBodyLine(@"<tr><th>Property</th><th>Value</th></tr>");
                AddBodyLine(@"<tr class=""evenRow""><td>Name</td><td>" + ScrubHtml(_trigger.Name) + @"</td></tr>");
                AddBodyLine(@"<tr class=""oddRow""><td>Insert</td><td>" + _trigger.IsInsert.ToString() + @"</td></tr>");
                AddBodyLine(@"<tr class=""evenRow""><td>Update</td><td>" + _trigger.IsUpdate.ToString() + @"</td></tr>");
                AddBodyLine(@"<tr class=""oddRow""><td>Delete</td><td>" + _trigger.IsDelete.ToString() + @"</td></tr>");
                AddBodyLine(@"<tr class=""evenRow""><td>Instead</td><td>" + _trigger.IsInsert.ToString() + @"</td></tr>");
                AddBodyLine(@"<tr class=""oddRow""><td>Is System Object</td><td>" + _trigger.IsMicrosoftShipped.ToString() + @"</td></tr>");
                
                string s = @"<tr class=""evenRow""><td>Is Enabled</td><td>";
                s += _trigger.IsDisabled==false?"True":"False";
                s += @"</td></tr>";
                AddBodyLine(s);

                AddBodyLine(@"<tr class=""oddRow""><td>Creation Date</td><td>" + _trigger.CreateDate.ToString() + @"</td></tr>");

                s = @"<tr class=""evenRow""><td>Description</td><td>";
                s += _trigger.Description == "" ? @"nbsp;" : ScrubHtml(_trigger.Description);
                s += @"</td></tr>";
                AddBodyLine(s);

                AddBodyLine(@"</table>");
                AddBodyLine(@"</div>");
                #endregion
                #region Extended Properties
                AddBodyLine(@"<h2>");
                AddBodyLine(@"<img id=""img_extendedProperties"" src=""" + RootFolderPath + @"minus.jpg""> ");
                AddBodyLine(@"<a href=""javascript:;"" onmousedown=""toggleDiv('extendedProperties');"">Extended Properties</a>");
                AddBodyLine(@"</h2>");

                AddBodyLine(@"<div id=""extendedProperties"">");
                AddBodyLine(@"<table>");
                AddBodyLine(@"<colgroup><col class=""nameColumn"" /><col class=""valueColumn"" /></colgroup>");
                AddBodyLine(@"<tr><th>Parent</th><th>Value</th></tr>");

                evenRow = true;
                foreach (SqlServer.ExtendedProperty extProperty in _trigger.ExtendedProperties)
                {
                    string row = evenRow == true ? @"<tr class=""evenRow"">" : @"<tr>";
                    evenRow = !evenRow;
                    row += @"<td>" + ScrubHtml(extProperty.Name) + @"</td><td>" + ScrubHtml(extProperty.Value) + @"</td></tr>";
                    AddBodyLine(row);
                }

                AddBodyLine(@"</table>");
                AddBodyLine(@"</div>");
                #endregion
                #region DDL
                AddBodyLine(@"<h2>");
                AddBodyLine(@"<img id=""img_ddl"" src=""" + RootFolderPath + @"minus.jpg""> ");
                AddBodyLine(@"<a href=""javascript:;"" onmousedown=""toggleDiv('ddl');"">DDL</a>");
                AddBodyLine(@"</h2>");

                AddBodyLine(@"<div id=""ddl"">");
                AddBodyLine(@"<table class=""sqlCode"">");

                evenRow = true;
                var codeLines = ColorizeDDL(_trigger.GetDdlTokens());
                foreach (string codeLine in codeLines)
                {
                    string code = codeLine == "" ? @"&nbsp;" : codeLine;
                    if (evenRow == true)
                        AddBodyLine(@"<tr class=""evenRow""><td>" + code + @"</td></tr>");
                    else
                        AddBodyLine(@"<tr class=""oddRow""><td>" + code + @"</td></tr>");
                    evenRow = !evenRow;
                }
                AddBodyLine(@"</table>");
                AddBodyLine(@"</div>");
                #endregion

                AddBodyLine(@"</div>");
                _html = CreateHtmlDocument();
            }
            return _html;
        }
        #endregion
        #region Private
        private SqlServer.Trigger _trigger;
        #endregion
    }
}
